<?php 
namespace app\App\controller;

use app\App\validate\UserValidate;
use plugin\saiadmin\basic\AppBaseController;
use support\Request;
use support\Response;
use \app\App\logic\UserLogic;
use support\Log;

// APP用户模块
class UserController extends AppBaseController{

     /**
     * 不需要登录的方法, 就不存在 $this->userId哦
     */
    protected array $noNeedLogin = ['captcha', 'register', 'sendCaptchaMsg', 'login', 'resetPwd', 'loginCaptcha'];

    public function __construct()
    {
        $this->validate = new UserValidate();
        $this->logic = new UserLogic();
        parent::__construct();
    }

    // 图形验证码
    public function captcha() : Response
    {
        $result = $this->logic->captcha();
        if ($result['result'] !== 1) {
            return $this->fail($result['message']);
        }
        return $this->success($result);
    }

    /**
     * 账号密码登录
     * @param Request $request
     * @return Response
     */
    public function login(Request $request)
    {
        if(!$this->validate->scene('login')->check($request->post())){
             return $this->fail($this->validate->getError());
        }
        if(!$this->logic->checkCaptchaImage($request)){
            return $this->fail(trans('user.img_captcha_fail'));
        }
        $username = $request->post('username', '');
        $password = $request->post('password', '');

        $resultToken =  $this->logic->login($username, $password);
        if($resultToken){
            return $this->success($resultToken);
        }
        return $this->fail(trans('user.login_info_fail'));
    }

    // 短信/邮箱 验证码登录
    public function loginCaptcha(Request $request){
        if(!$this->validate->scene('loginCaptcha')->check($request->post())){
            return $this->fail($this->validate->getError());
        }
        if(!$this->logic->checkCaptchaImage($request)){
            return $this->fail(trans('user.img_captcha_fail'));
        }
        $email = $request->post('email', null);
        $phone = $request->post('phone', null);
        $captcha_content = $request->post('captcha', ''); //短信验证码或邮箱验证码内容
        
        if(empty($email)&&empty($phone)){
            return $this->fail(trans('common.param_error'));
        }

        $resultToken =  $this->logic->loginCaptcha($email, $phone, $captcha_content);
        if($resultToken){
            return $this->success($resultToken);
        }
        return $this->fail(trans('user.login_info_fail'));
    }

    // 退出登录
    public function logout(Request $request){
        return $this->success([
            'data' => $this->logic->removeToken($this->userId)
        ]);
    }

    // 修改头像
    public function changeAvatar($request){
        $file = current(request()->file());
        Log::info('changeAvatar 获取参数', $request->post());
        // $file = $request->post('file',null);
        if(hasValue($file)){
            return $this->success([
                'data' => $this->logic->changeAvatar(),
            ]);
        }

        return $this->fail(trans('user.upload_lack_file'));
    }

    // 修改昵称
    public function changeNickname($request){
        if(!$this->validate->scene('newNickName')->check($request->post())){
            return $this->fail($this->validate->getError());
        }
        $newNickName = $request->post('newNickName', '');
        return $this->success( $this->logic->changeNickname($newNickName)->toArray() );
    }

    // 修改密码
    public function resetPwd($request){
        if(!$this->validate->scene('resetPwd')->check($request->post())){
            return $this->fail($this->validate->getError());
        }
        if(!$this->logic->checkCaptchaImage($request)){
            return $this->fail(trans('user.img_captcha_fail'));
        }

        $email = $request->post('email', null);
        $phone = $request->post('phone', null);
        $password = $request->post('password', ''); // 经过了前端MD5加密一次(前端不一定有HTTPS)
        $captcha_content = $request->post('captcha', ''); //短信验证码或邮箱验证码内容

        if(empty($email)&&empty($phone)){
            return $this->fail(trans('common.param_error'));
        }

        $result = $this->logic->changePwd($this->userId, $email, $phone, $password, $captcha_content);
        if($result){
            return $this->success([
                'data' => trans('common.success')
            ]);
        }else{
            return $this->fail(trans('common.fail'));
        }
    }


    // 获取用户信息
    function info($request){
        $userId = $this->userId;
        $userInfo = $this->logic->info($userId);
        return $this->success([
            'info' => $userInfo
        ]);
    }

    // 刷新token
    function refreshToken($request){
        return $this->success([
            'token' => $this->logic->grandLoginToken($this->userId, $this->userName),
        ]);
    }

    // 注册
    public function register(Request $request){
        if(!$this->validate->scene('register')->check($request->post())){
            return $this->fail($this->validate->getError());
        }
        if(!$this->logic->checkCaptchaImage($request)){
            return $this->fail(trans('user.img_captcha_fail'));
        }
        $email = $request->post('email', null);
        $phone = $request->post('phone', null);
        $password = $request->post('password', ''); // 经过了前端MD5加密一次(前端不一定有HTTPS)
        $captcha_content = $request->post('captcha', ''); //短信验证码或邮箱验证码内容

        if(empty($email)&&empty($phone)){
            return $this->fail(trans('common.param_error'));
        }

        $result = $this->logic->register($email, $phone, $password, $captcha_content);
        if($result['success']){
            return $this->success([
                'data' => $result['content']
            ]);
        }else{
            return $this->fail($result['content']);
        }
    }

    // 发送短信或邮件验证码
    public function sendCaptchaMsg(Request $request){
        if(!$this->validate->scene('captcha')->check($request->post())){
            return $this->fail($this->validate->getError());
        }
        if(!$this->logic->checkCaptchaImage($request)){
            return $this->fail(trans('user.img_captcha_fail'));
        }

        $email = $request->post('email', null);
        $phone = $request->post('phone', null);
        $type = $request->post('type', '');     //发送类型, 1-短信验证，2-邮件验证
        $scene = $request->post('scene', '');   //场景值, 1-注册，2-重置密码，3-绑定邮箱，4-绑定手机，5-修改邮箱，6-修改手机
        if(empty($type) || empty($scene)|| (empty($email)&&empty($phone))){
            return $this->fail(trans('common.param_error'));
        }

        $result = $this->logic->sendCaptchaMsg($type, $scene, $email, $phone);

        if($result['success']){
            return $this->success([
                'data' => trans('common.success')
            ]);
        }

        return $this->fail($result['content']);
    }


}
